/*
 @Date: 2022/4/29
 @Desc: 相册
 */

import UIKit
import DZNEmptyDataSet

class IPAblumViewController: UIViewController {

    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureCollectionView()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true
    }
}

//MARK: UICollectionViewDataSource UICollectionViewDelegate
extension IPAblumViewController: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let list = UserDefaults.standard.object(forKey: "saveList")as? [Data] {
            return list.count
        }else{
            return 0
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: IPAblumCell.reuseId, for: indexPath) as! IPAblumCell
        if let list = UserDefaults.standard.object(forKey: "saveList")as? [Data] {
            cell.img.image = UIImage(data: list[indexPath.item])
        }
//        let list = UserDefaults.standard.object(forKey: "saveList")as! NSMutableArray
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let detail = IPAblumDetailViewController()
        let list = UserDefaults.standard.object(forKey: "saveList")as? [Data]
        detail.img = UIImage(data: list![indexPath.item])!
        self.navigationController?.pushViewController(detail, animated: true)
    }
}

//MARK: DZNEmptyDataSetSource
extension IPAblumViewController: DZNEmptyDataSetSource {
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let str = "空空如也～"
        let dict: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor.ip_text_color]
        return NSAttributedString.init(string: str, attributes: dict)
    }
}

private extension IPAblumViewController {
    
    func configureCollectionView(){
        
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        registerView()
        calculateCellSize()
    }
    
    func registerView(){
        let cellNib = UINib.init(nibName: "IPAblumCell", bundle: .main)
        collectionView.register(cellNib, forCellWithReuseIdentifier: IPAblumCell.reuseId)
    }
    
    func calculateCellSize(){
        let sectionEdge: CGFloat = 10
        let sp: CGFloat = 12
        /// 一排3个
        let num = 3
        let totalWidth = view.bounds.width
        let scale = 130.0 / 110.0
        let width = (UIScreen.main.bounds.size.width-44) / 3
        let height = width * scale
        let size = CGSize.init(width: (UIScreen.main.bounds.size.width-44)/3, height: height)
        flowLayout.itemSize = size
        flowLayout.minimumLineSpacing = 20
        flowLayout.minimumInteritemSpacing = 12
        flowLayout.sectionInset = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
    }
}
